package cayte.xiami.in.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class PullToRefreshListView extends PullToRefreshBase<ListView> {

	public PullToRefreshListView(Context context) {
		super(context);
	}

	public PullToRefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected ListView createAdapterView(Context context, AttributeSet attrs) {
		ListView lv = new ListView(context, attrs);
		lv.setVerticalFadingEdgeEnabled(true);
		lv.setFadingEdgeLength(10);
		lv.setCacheColorHint(Color.WHITE);
		lv.setSelector(android.R.color.transparent);

		// Set it to this so it can be used in ListActivity/ListFragment
		lv.setId(android.R.id.list);
		return lv;
	}

	public void destroy() {
		// TODO Auto-generated method stub
		setRefreshableViewNull();
	}

	public void setAdapter(ListAdapter adapter) {
		// TODO Auto-generated method stub
		getRefreshableView().setAdapter(adapter);
	}

	public void startRefreshing() {
		setRefreshing();
	}

	public void endRefreshing() {
		resetHeader();
	}

	public boolean isRefreshing() {
		return isRefresh();
	}

	public void setOnItemClickListener(final OnItemClickListener l) {
		getRefreshableView().setOnItemClickListener(
				new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						l.onItemClick(arg0, arg1, arg2, arg3);
					}
				});
	}

	public void setOnItemLongClickListener(final OnItemLongClickListener l) {
		getRefreshableView().setOnItemLongClickListener(
				new AdapterView.OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> arg0,
							View arg1, int arg2, long arg3) {
						// TODO Auto-generated method stub
						l.onItemLongClick(arg0, arg1, arg2, arg3);
						return false;
					}
				});
	}

	public void setOnItemSelectedListener(final OnItemSelectedListener l) {
		getRefreshableView().setOnItemSelectedListener(
				new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						l.onItemSelected(arg0, arg1, arg2, arg3);
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						l.onNothingSelected(arg0);
					}

				});
	}
}
